package kgame5.kengine {
import kgame5.kengine.gameunit.GameUnit;

import flash.utils.Dictionary;

/**
 * @author kk
 * 单例模式，这个是为了每个gameUnit都能方便的取得游戏世界中的原件
 * 需要做成单例模式，保证在程序的每一处都能方便的取得和使用。就跟粒子效果和SoundManager一样，到处都可用
 * 
 * 只负责维持各个gu的引用，不负责逻辑
 * 就是个数值持有者。
 * 主要方法是getList(),getListByClassifyID(),getListByType()
 */
public class EngineContext {
	
	private var dict_all:Dictionary;//guList[guID]=GameUnit
	
	//根据classify分类
	private var dict_byClassifyID:Dictionary;//dict[classifyID]=Vector.<GameUnit>;
	//根据typeID分类的集合
	private var dict_byTypeID:Dictionary;//dict[typeID]=Vector.<GameUnit>;
	
	public function EngineContext(ss:SinglonEnforce){
		//单例
	}
	
	private static var instance:EngineContext;
	public static function getInstance():EngineContext{
		if(instance==null){
			instance=new EngineContext(new SinglonEnforce());
		}
		return instance;
	}
	
	public function init():void{
		dict_all=new Dictionary();
		dict_byClassifyID=new Dictionary();
		dict_byTypeID=new Dictionary();
	}
	
	public function clear():void{
		dict_all=null;
		dict_byClassifyID=null;
		dict_byTypeID=null;
	}
	
	//================常用的====================
	public function getList():Dictionary{
		return dict_all;
	}
	
	public function getListByClassfiyID(classifyID:int):Dictionary{
		return dict_byClassifyID[classifyID];
	}
	
	public function getListByTypeID(typeID:int):Dictionary{
		return dict_byTypeID[typeID];
	}
	
	//===================增加删除之类的==================
	public function addGU(gu:GameUnit):void{
		if(dict_all[gu.getID()]!=null){
			throw new Error("已经被添加了!!");
		}
		
		//总类表中添加
		dict_all[gu.getID()]=gu;
		
		//
		var dict_classify:Dictionary=dict_byClassifyID[gu.getClassifyID()];//按类分
		if(dict_classify==null){
			dict_classify=new Dictionary();
			dict_byClassifyID[gu.getClassifyID()]=dict_classify;
		}
		dict_classify[gu.getID()]=gu;
		
		var dict_type:Dictionary=dict_byTypeID[gu.getTypeID()];//按类分
		if(dict_type==null){
			dict_type=new Dictionary();
			dict_byTypeID[gu.getTypeID()]=dict_type;
		}
		dict_type[gu.getID()]=gu;
		
	}
	
	public function findGU(guID:int):GameUnit{
		return dict_all[guID];
	}
	
	//返回是否删除成功
	public function removeGU(guID:int):Boolean{
		
		var gu:GameUnit=dict_all[guID];
		if(gu==null){
			return false;
		}
		//总类表中删除
		delete dict_all[guID];
		
		//
		var dict_classify:Dictionary=dict_byClassifyID[gu.getClassifyID()];//按类分
		if(dict_classify!=null){
			delete dict_classify[guID];
		}
		
		//
		var dict_type:Dictionary=dict_byTypeID[gu.getTypeID()];//按类分
		if(dict_type==null){
			delete dict_type[guID];
			
		}
		
		return true;
	}
//	//actorList
//	public var actorList:Vector.<GUActor>;
//	//barList 障碍物列表
//	public var barList:Vector.<GameUnit>;
}
}
class SinglonEnforce{
}
	